<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY nbsp "&#x00A0;">
]>
<xsl:stylesheet
  version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:msxml="urn:schemas-microsoft-com:xslt"
	xmlns:umbraco.library="urn:umbraco.library"
	xmlns:tagsLib="urn:tagsLib"
	xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings"
	xmlns:BlogLibrary="urn:BlogLibrary"
  xmlns:Blog="urn:Blog"
	exclude-result-prefixes="msxml umbraco.library tagsLib Exslt.ExsltStrings BlogLibrary Blog">

  <xsl:output method="xml" omit-xml-declaration="yes"/>

  <xsl:param name="currentPage"/>

  <!-- Numeric - set the page size for displaying results.  Defaults to 10 -->
  <xsl:variable name="mPageSize" select="/macro/pageSize"/>
  <!-- used in the navigation id - e.g. 'above' or 'below' -->
  <xsl:variable name="navPosition" select="/macro/position"/>

  <xsl:variable name="currentBlog" select="Blog:XmlCurrent(number($currentPage/@id))" />

  <xsl:variable name="pageSize">
    <xsl:choose>
      <xsl:when test="$mPageSize &lt;= 0 or string($mPageSize) = 'NaN'">10</xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$mPageSize"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="pageNumber">
    <xsl:choose>
      <xsl:when test="umbraco.library:RequestQueryString('page') &lt;= 0 or string(umbraco.library:RequestQueryString('page')) = '' or string(umbraco.library:RequestQueryString('page')) = 'NaN'">1</xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="umbraco.library:RequestQueryString('page')"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="filter">
    <xsl:choose>
      <xsl:when test="string-length(umbraco.library:Request('filterby')) &gt; 0">
        <xsl:value-of select="umbraco.library:Request('filterby')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="''"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="numberOfRecords">
    <xsl:choose>
      <xsl:when test="$filter = ''">
        <xsl:value-of select="count($currentBlog//*[(@isDoc or name()='node') and BlogLibrary:IsType('BlogPost',@id)])"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="count($currentBlog//*[(@isDoc or name()='node') and BlogLibrary:IsType('BlogPost',@id) and contains(Exslt.ExsltStrings:lowercase(./* [@alias='tags' or name()='tags']), Exslt.ExsltStrings:lowercase($filter))])"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:template match="/">
    <xsl:if test="$numberOfRecords &gt; $pageSize">
      <xsl:call-template name="navBar">
        <xsl:with-param name="position" select="$navPosition"/>
      </xsl:call-template>
    </xsl:if>

  </xsl:template>

  <xsl:template name="navBar">
    <xsl:param name="position"/>
    <nav id="nav-{$position}" class="navigation">
      <xsl:if test="$pageNumber &gt; 1">
        <div class="nav-previous">
          <a href="{umbraco.library:NiceUrl($currentPage/@id)}?page={$pageNumber - 1}&amp;filterby={$filter}" title="Previous Page">previous</a>
        </div>
      </xsl:if>
      <div class="nav-pages">
        <xsl:call-template name="pager">
          <xsl:with-param name="i">1</xsl:with-param>
          <xsl:with-param name="page" select="$pageNumber"></xsl:with-param>
          <xsl:with-param name="count" select="ceiling($numberOfRecords div $pageSize)"></xsl:with-param>
        </xsl:call-template>
      </div>
      <xsl:if test="(($pageNumber) * $pageSize) &lt; ($numberOfRecords)">
        <div class="nav-next">
          <a href="{umbraco.library:NiceUrl($currentPage/@id)}?page={$pageNumber + 1}&amp;filterby={$filter}" title="Next Page">next</a>
        </div>
      </xsl:if>
    </nav>
  </xsl:template>

  <xsl:template name="pager">
    <xsl:param name="i"/>
    <xsl:param name="count"/>
    <xsl:param name="page"/>
    <xsl:if test="$i &lt;= $count">
      <xsl:if test="$page != $i">
        <a href="{umbraco.library:NiceUrl($currentPage/@id)}?page={$i}&amp;filterby={$filter}" title="Go to page {$i} of {$count}" >
          <xsl:value-of select="$i" />
        </a>
      </xsl:if>
      <xsl:if test="$page = $i">
        <span class="currentPage">
          <xsl:value-of select="$i" />
        </span>
      </xsl:if>
    </xsl:if>
    <xsl:if test="$i &lt;= $count">
      <xsl:call-template name="pager">
        <xsl:with-param name="i">
          <xsl:value-of select="$i + 1"/>
        </xsl:with-param>
        <xsl:with-param name="count">
          <xsl:value-of select="$count"/>
        </xsl:with-param>
        <xsl:with-param name="page">
          <xsl:value-of select="$page"/>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>